package pers.cz.postgirl.exception;

import java.text.MessageFormat;

/**
 * 基本异常类,该类是为了方便后期扩展的
 * @program: PostGirl-panent
 * @description: BaseException
 * @author: Cheng Zhi
 * @create: 2021-03-11 14:13
 **/
public class BaseException extends RuntimeExceptionWithCode {

    private Object[] formatObjects = null;

    public BaseException(String messageCode) {
        super(messageCode);
    }

    public BaseException() {
        super();
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    public BaseException(String messageCode, Object... objects) {
        super(messageCode, null, objects);
        this.formatObjects = objects;

    }
    public BaseException(Throwable cause) {
        super(cause);
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ":" + getExceptionDescribe();
    }

    private String getExceptionDescribe() {

        String errorCode = super.getMessageCode();

        String template = ErrorCodeHolder.getInstance().get(errorCode);

        // 将模板中的占位符替换掉
        if (formatObjects == null || formatObjects.length == 0) {
            return template;
        } else {
            return MessageFormat.format(template, formatObjects);
        }
    }
}
